<?php

namespace app\admin\command;

use app\api\model\WxMessage;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;


class Notice extends Command
{

    protected function configure()
    {
        $this->setName('notice')->setDescription('预售推送定时器');
    }

    protected function execute(Input $input, Output $output)
    {
        $model = Db::name('order_goods')->where(['goods_type'=>4,'notice_status'=>0,'goods_notice_time'=>['<',time()]])->where(['goods_notice_time'=>['>',0]])->select();

        $user_model = Db::name('user');
        $order_model = Db::name('order');
        $order_goods_model = Db::name('order_goods');
        if(!empty($model)){
            foreach ($model as $value) {
               $open_id = $user_model ->where('user_id',$value['user_id'])->value('open_id');
                $order_info = $order_model->where('order_id',$value['order_id'])->find();
               if(!$open_id){
                   log_print('用户缺少open_id');
                   continue;
               }
                $data = [
                    'openid'=>$open_id,
                    'name'=>$value['goods_name'],
                    'mobile'=>15819406810,
                    'order_str'=>$order_info['order_no'],
                    'order_status'=>'等待使用',
                    'sum_money'=>$value['total_price']
                ];
                $order_goods_model->where('order_goods_id',$value['order_goods_id'])->update(['notice_status'=>1]);
                WxMessage::sendMessage(11,$data);
            }
        }
        $output->writeln("通知完成");

    }

}